//
//  KATLabelButton.m
//  KATFramework
//
//  Created by Kantice on 16/7/28.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATLabelButton.h"



@interface KATLabelButton ()

///点击手势
@property(nonatomic,retain) UITapGestureRecognizer *tap;

///长按手势
@property(nonatomic,retain) UILongPressGestureRecognizer *press;

@end



@implementation KATLabelButton


#pragma -mark 类方法



//获取实例
+ (instancetype)buttonWithFrame:(CGRect)frame text:(NSString *)text font:(UIFont *)font color:(UIColor *)color andAction:(void (^)(void))action
{
    UILabel *label=[[[UILabel alloc] init] autorelease];
    label.frame=frame;
    label.text=text;
    label.numberOfLines=0;
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=color;
    label.layer.borderColor=color.CGColor;
    label.backgroundColor=KCOLOR_CLEAR;
    label.font=font;
    
    return [self buttonWithLabel:label andAction:action];
}


//通过Label获取实例
+ (instancetype)buttonWithLabel:(UILabel *)label andAction:(void (^)(void))action
{
    if(!label)
    {
        return nil;
    }
    
    KATLabelButton *button=[[[self alloc] init] autorelease];
        
    button.label=label;
    button.disabled=NO;
    button.eventDelegate=nil;
    button.disabledAlpha=0.4;
    button.onClickAnimation=[button animationToOpacity:0.4 withDuration:0.2 repeat:1 autoreverses:YES delay:0.0];
    
    button.type=0;
    button.num=0;
    button.index=0;
    button.value=0.0;
    button.message=nil;
    button.url=nil;
    button.object=nil;
    
    //点击事件
    button.tap=[[[UITapGestureRecognizer alloc] initWithTarget:button action:@selector(buttonTaped)] autorelease];
    button.tap.numberOfTapsRequired=1;
    
    //长按事件
    button.press=[[[UILongPressGestureRecognizer alloc] initWithTarget:button action:@selector(buttonPressed:)] autorelease];
    button.press.minimumPressDuration=0.3;
    
    button.onClickAction=action;
    
    return button;
}


#pragma -mark 对象方法


//设置frame
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _label.frame=self.bounds;
}


//设置标签
- (void)setLabel:(UILabel *)label
{
    if(label && (_label!=label))
    {
        if(_label)
        {
            [_label removeFromSuperview];
            [_label release];
            
            _label=nil;
        }
        
        _label=[label retain];
        
        self.frame=_label.frame;
        _label.frame=self.bounds;
        
        [self addSubview:_label];
    }
}


//禁用启用
- (void)setDisabled:(BOOL)disabled
{
    _disabled=disabled;
    
    if(_disabled)
    {
        self.alpha=_disabledAlpha;
    }
    else
    {
        self.alpha=1.0;
    }
}


//设置点击事件
- (void)setOnClickAction:(void (^)(void))onClickAction
{
    if(_onClickAction)
    {
        Block_release(_onClickAction);
    }
    
    if(onClickAction)
    {
        _onClickAction=[onClickAction copy];
        
        //添加点击事件
        self.userInteractionEnabled=YES;
        
        [self addGestureRecognizer:_tap];
        [self addGestureRecognizer:_press];
    }
    else
    {
        _onClickAction=nil;
        
        if(!_eventDelegate)//无代理
        {
            //移除点击事件
            [self removeGestureRecognizer:_tap];
            [self removeGestureRecognizer:_press];
            
            self.userInteractionEnabled=NO;
        }
    }
}


//设置代理
- (void)setEventDelegate:(id<KATLabelButtonDelegate>)eventDelegate
{
    _eventDelegate=eventDelegate;
    
    if(_eventDelegate)//有代理
    {
        //添加点击事件
        self.userInteractionEnabled=YES;
        
        [self addGestureRecognizer:_tap];
        [self addGestureRecognizer:_press];
    }
    else
    {
        if(!_onClickAction)//没有点击block
        {
            //移除点击事件
            [self removeGestureRecognizer:_tap];
            [self removeGestureRecognizer:_press];
            
            self.userInteractionEnabled=NO;
        }
    }
}


//点击事件
- (void)buttonTaped
{
    if(!_disabled)
    {
        if(self.animating)
        {
            [self clearAnimations];
        }
        
        if(_onClickAnimation)
        {
            [self restoreSprite];
            [self startAnimation:_onClickAnimation andDelay:0];
        }
        
        if(_onClickAction)
        {
            _onClickAction();
        }
        
        if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(labelButtonTaped:)])
        {
            [_eventDelegate labelButtonTaped:self];
        }
    }
}


//长按事件
- (void)buttonPressed:(UILongPressGestureRecognizer *)recognizer
{
    if(!_disabled)
    {
        if(recognizer.state==UIGestureRecognizerStateBegan)//开始，变色
        {
            self.alpha=0.4;
        }
        else if(recognizer.state==UIGestureRecognizerStateEnded)//完成
        {
            self.alpha=1.0;
            
            CGPoint point=[recognizer locationInView:self];
            
            if(point.x<0.0 || point.x>self.bounds.size.width || point.y<0.0 || point.y>self.bounds.size.height)//超出边界
            {
                
            }
            else
            {
                //回调
                if(_onClickAction)
                {
                    _onClickAction();
                }
                
                if(_eventDelegate && [_eventDelegate respondsToSelector:@selector(labelButtonTaped:)])
                {
                    [_eventDelegate labelButtonTaped:self];
                }
            }
        }
        else if(recognizer.state==UIGestureRecognizerStateChanged)//改变
        {
            CGPoint point=[recognizer locationInView:self];
            
            if(point.x<0.0 || point.x>self.bounds.size.width || point.y<0.0 || point.y>self.bounds.size.height)//超出边界
            {
                self.alpha=1.0;
            }
            else
            {
                self.alpha=0.4;
            }
        }
        else
        {
            self.alpha=1.0;
        }
    }
}


//释放内存
- (void)dealloc
{
    if(_onClickAction)
    {
        Block_release(_onClickAction);
        
        _onClickAction=nil;
    }
    
    [_label release];
    [_onClickAnimation release];
    
    [_tap release];
    [_press release];
    
    [_message release];
    [_url release];
    [_object release];
    
    [super dealloc];
}

@end
